<?php
/**
 * 活动管理
 * @copyright (c) Seer All Rights Reserved
 */

require_once 'globals.php';

$Activity_Model = new Activity_Model();
$action = $_POST['action'];
$mode = $_POST['mode'];
//显示活动一览
if ($action == '') {

	$page = isset($_GET['pid']) ? $_GET['pid'] : '1';

	$activity_name = isset($_GET['activity_name']) ? $_GET['activity_name'] : '';
	$activity_name = $activity_name?$activity_name:$_POST['activity_name'];

	$sqlSegment .= ' ORDER BY buildtime DESC ';

	$pwd = '活动管理';

	$addUrl = $activity_name ? "activity_name={$activity_name}&" : '';

	$activitiesNum = $Activity_Model->getActivitiesNum($activity_name);
	$activies = $Activity_Model->getActivitiesForAdmin($page, $activity_name);
	$pageurl =  pagination($activitiesNum, Option::get('admin_perpage_num'), $page, "activity.php?{$addUrl}pid=");

	 include View::getView('header');
	 require_once View::getView('activity/activity_list');
	 include View::getView('footer');View::output();
}

//显示活动一览
if ($action == 'del') {
	$acts = isset($_POST['ids']) ? $_POST['ids'] : array();
	$Activity_Model->deleteActivity($acts);
	seerDirect("./activity.php?active_del=1&{$addUrl}");
}

$pic_type = array('gif', 'jpg', 'jpeg','png');
$imgSize =array();
// 活动添加
if ($action == 'add') {
	// 添加活动画面初期化
	if(!$mode)
	{
		include View::getView('header');
		require_once View::getView('activity/activity_update');
		include View::getView('footer');
		View::output();
	}
	// 添加活动信息提交
	else if($mode=="commit")
	{
		// 活动缩略图
		if ($_FILES['activity_cover']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_cover']['tmp_name']);
			if($imgSize[0]!=330 || $imgSize[1]!=330)
			{
				emMsg("活动缩略图图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：330px*330px");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_cover']['name'], $_FILES['activity_cover']['error'], $_FILES['activity_cover']['tmp_name'], $_FILES['activity_cover']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_cover = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_cover="http://".$_SERVER['HTTP_HOST']."/m/".$activity_cover;
			}
		}

		//活动海报1
		if ($_FILES['activity_poster1']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_poster1']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("活动海报1图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}


			$file_info = uploadFile($_FILES['activity_poster1']['name'], $_FILES['activity_poster1']['error'], $_FILES['activity_poster1']['tmp_name'], $_FILES['activity_poster1']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_poster1 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_poster1="http://".$_SERVER['HTTP_HOST']."/m/".$activity_poster1;
			}
		}

		//活动海报2
		if ($_FILES['activity_poster2']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_poster2']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("活动海报2图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_poster2']['name'], $_FILES['activity_poster2']['error'], $_FILES['activity_poster2']['tmp_name'], $_FILES['activity_poster2']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_poster2 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_poster2="http://".$_SERVER['HTTP_HOST']."/m/".$activity_poster2;
			}
		}
		//报名页面头部
		if ($_FILES['activity_sign_top']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_top']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名页面头部图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}


			$file_info = uploadFile($_FILES['activity_sign_top']['name'], $_FILES['activity_sign_top']['error'], $_FILES['activity_sign_top']['tmp_name'], $_FILES['activity_sign_top']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_top = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_top="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_top;
			}
		}
		//报名成功1
		if ($_FILES['activity_sign_suc_poster1']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_suc_poster1']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名成功1图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}



			$file_info = uploadFile($_FILES['activity_sign_suc_poster1']['name'], $_FILES['activity_sign_suc_poster1']['error'], $_FILES['activity_sign_suc_poster1']['tmp_name'], $_FILES['activity_sign_suc_poster1']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_suc_poster1 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_suc_poster1="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_suc_poster1;
			}
		}


		//报名成功2
		if ($_FILES['activity_sign_suc_poster2']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_suc_poster2']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名成功2图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}


			$file_info = uploadFile($_FILES['activity_sign_suc_poster2']['name'], $_FILES['activity_sign_suc_poster2']['error'], $_FILES['activity_sign_suc_poster2']['tmp_name'], $_FILES['activity_sign_suc_poster2']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_suc_poster2 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_suc_poster2="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_suc_poster2;
			}
		}

		$activity_name = $_POST['activity_name'];
		$activity_deadline = $_POST['activity_deadline']?$_POST['activity_deadline']:"2099-12-29";
		$activity_bgcolour = $_POST['activity_bgcolour'];
		$activity_sign_bgcolour = $_POST['activity_sign_bgcolour'];

		$activity = array(
			'activity_cover' => $activity_cover,
			'activity_poster1' => $activity_poster1,
			'activity_poster2' => $activity_poster2,
			'activity_sign_top' => $activity_sign_top,
			'activity_sign_suc_poster1' => $activity_sign_suc_poster1,
			'activity_sign_suc_poster2' => $activity_sign_suc_poster2,
			'activity_name' => $activity_name,
			'activity_deadline' => $activity_deadline,
			'activity_bgcolour' => $activity_bgcolour,
			'activity_sign_bgcolour' => $activity_sign_bgcolour,
		);
		$Activity_Model->addActivity($activity);
		seerDirect("./activity.php?active_add=1&activity_name={$activity_name}");
	}
}

if ($action == 'update') {
	$ID = $_POST['activity_ID'];

	// 编辑活动画面初期化
	if(!$mode)
	{
		$activity=$Activity_Model->getOneActivity($ID);
		$activity=$activity[0];
		include View::getView('header');
		require_once View::getView('activity/activity_update');
		include View::getView('footer');
		View::output();
	}
	// 添加活动信息提交
	else if($mode=="commit")
	{

		$pre_activity_cover= $_POST['pre_activity_cover'];
		$pre_activity_poster1= $_POST['pre_activity_poster1'];
		$pre_activity_poster2= $_POST['pre_activity_poster2'];
		$pre_activity_sign_top= $_POST['pre_activity_sign_top'];
		$pre_activity_sign_suc_poster1= $_POST['pre_activity_sign_suc_poster1'];
		$pre_activity_sign_suc_poster2= $_POST['pre_activity_sign_suc_poster2'];

		// 活动缩略图
		if ($_FILES['activity_cover']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_cover']['tmp_name']);
			if($imgSize[0]!=330 || $imgSize[1]!=330)
			{
				emMsg("活动缩略图图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：330px*330px");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_cover']['name'], $_FILES['activity_cover']['error'], $_FILES['activity_cover']['tmp_name'], $_FILES['activity_cover']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_cover = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_cover="http://".$_SERVER['HTTP_HOST']."/m/".$activity_cover;
			}
			if(file_exists($pre_activity_cover))
			{
				unlink($pre_activity_cover);
			}
		}
		else
		{
			$activity_cover	=$pre_activity_cover;
		}


		//活动海报1
		if ($_FILES['activity_poster1']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_poster1']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("活动海报1图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_poster1']['name'], $_FILES['activity_poster1']['error'], $_FILES['activity_poster1']['tmp_name'], $_FILES['activity_poster1']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_poster1 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_poster1="http://".$_SERVER['HTTP_HOST']."/m/".$activity_poster1;
			}
			if(file_exists($pre_activity_poster1))
			{
				unlink($pre_activity_poster1);
			}
		}
		else
		{
			$activity_poster1 =$pre_activity_poster1;
		}


		//活动海报2
		if ($_FILES['activity_poster2']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_poster2']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("活动海报2图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_poster2']['name'], $_FILES['activity_poster2']['error'], $_FILES['activity_poster2']['tmp_name'], $_FILES['activity_poster2']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_poster2 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_poster2="http://".$_SERVER['HTTP_HOST']."/m/".$activity_poster2;
			}
			if(file_exists($pre_activity_poster2))
			{
				unlink($pre_activity_poster2);
			}
		}
		else
		{
			$activity_poster2 =$pre_activity_poster2;
		}

		//报名页面头部
		if ($_FILES['activity_sign_top']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_top']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名页面头部图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_sign_top']['name'], $_FILES['activity_sign_top']['error'], $_FILES['activity_sign_top']['tmp_name'], $_FILES['activity_sign_top']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_top = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_top="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_top;
			}
			if(file_exists($pre_activity_sign_top))
			{
				unlink($pre_activity_sign_top);
			}
		}
		else
		{
			$activity_sign_top =$pre_activity_sign_top;
		}

		//报名成功1
		if ($_FILES['activity_sign_suc_poster1']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_suc_poster1']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名成功1图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}


			$file_info = uploadFile($_FILES['activity_sign_suc_poster1']['name'], $_FILES['activity_sign_suc_poster1']['error'], $_FILES['activity_sign_suc_poster1']['tmp_name'], $_FILES['activity_sign_suc_poster1']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_suc_poster1 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_suc_poster1="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_suc_poster1;
			}
			if(file_exists($pre_activity_sign_suc_poster1))
			{
				unlink($pre_activity_sign_suc_poster1);
			}
		}
		else
		{
			$activity_sign_suc_poster1	=$pre_activity_sign_suc_poster1;
		}

		//报名成功2
		if ($_FILES['activity_sign_suc_poster2']['size'] > 0) {

			$imgSize=getimagesize($_FILES['activity_sign_suc_poster2']['tmp_name']);
			if($imgSize[0]!=960)
			{
				emMsg("报名成功2图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：960px*Xpx");
				exit;
			}

			$file_info = uploadFile($_FILES['activity_sign_suc_poster2']['name'], $_FILES['activity_sign_suc_poster2']['error'], $_FILES['activity_sign_suc_poster2']['tmp_name'], $_FILES['activity_sign_suc_poster2']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$activity_sign_suc_poster2 = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$activity_sign_suc_poster2="http://".$_SERVER['HTTP_HOST']."/m/".$activity_sign_suc_poster2;
			}
			if(file_exists($pre_activity_sign_suc_poster2))
			{
				unlink($pre_activity_sign_suc_poster2);
			}
		}
		else
		{
			$activity_sign_suc_poster2	=$pre_activity_sign_suc_poster2;
		}

		$activity_name = $_POST['activity_name'];
		$activity_ID = $_POST['ID'];
		$activity_deadline = $_POST['activity_deadline']?$_POST['activity_deadline']:"2099-12-29";
		$activity_bgcolour = $_POST['activity_bgcolour'];
		$activity_sign_bgcolour = $_POST['activity_sign_bgcolour'];




		$activity = array(
			'activity_cover' => $activity_cover,
			'activity_poster1' => $activity_poster1,
			'activity_poster2' => $activity_poster2,
			'activity_sign_top' => $activity_sign_top,
			'activity_sign_suc_poster1' => $activity_sign_suc_poster1,
			'activity_sign_suc_poster2' => $activity_sign_suc_poster2,
			'activity_ID' => $activity_ID,
			'activity_name' => $activity_name,
			'activity_deadline' => $activity_deadline,
			'activity_bgcolour' => $activity_bgcolour,
			'activity_sign_bgcolour' => $activity_sign_bgcolour,
		);
		$Activity_Model->updateActivity($activity);
		seerDirect("./activity.php?active_update=1&activity_name={$activity_name}");
	}
}

